package com.yinda.dt.produce.app;

import com.xcgame.oh.SyncOkHttp;
import com.yinda.dt.produce.utils.DesUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/1.
 */

public class MySyncOkHttp extends SyncOkHttp {

    public MySyncOkHttp() {
        super(15, 30, 30);
    }

    public JSONObject post(String url, JSONObject jsonRequest) {
        Map<String, String> map = new HashMap<>();
        try {
            jsonRequest.put("phoneType", "android");
            map.put("request", URLEncoder.encode(new DesUtil("dtsbsbdsb", true).encode(URLEncoder.encode(jsonRequest.toString(), "utf-8")), "utf-8"));

            String responseString = super.post(url, map);
            if (responseString == null) {
                return null;
            }

            JSONObject jsonResponse = new JSONObject(responseString);
            String jsonString = URLDecoder.decode(new DesUtil("dtsbsbdsb", true).decode(jsonResponse.getString("result")), "utf-8");
            JSONObject jsonResult = new JSONObject(jsonString);
            return jsonResult;
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public JSONObject uploadFile(String url, JSONObject jsonRequest, File file) {

        Map<String, String> map = new HashMap<>();
        try {
            jsonRequest.put("phoneType", "android");
            map.put("request", URLEncoder.encode(new DesUtil("dtsbsbdsb", true).encode(URLEncoder.encode(jsonRequest.toString(), "utf-8")), "utf-8"));

            String responseString = super.uploadFile(url, map, file);
            if (responseString == null) {
                return null;
            }

            JSONObject jsonResponse = new JSONObject(responseString);
            String jsonString = URLDecoder.decode(new DesUtil("dtsbsbdsb", true).decode(jsonResponse.getString("result")), "utf-8");
            JSONObject jsonResult = new JSONObject(jsonString);
            return jsonResult;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
